package com.haiwei.poi.word.test;

import com.haiwei.poi.word.bean.HaiweiBeanUtil;
import com.haiwei.poi.word.common.HaiweiDocumentGenerator;
import com.haiwei.poi.word.loop.HaiweiLoopSginParser;
import com.haiwei.poi.word.loop.HaiweiLoopSign;
import com.haiwei.poi.word.loop.HaiweiLoopStack;
import com.haiwei.poi.word.poi.HaiweiXWPFTableUtil;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import java.io.File;
import java.util.*;

/**
 * 1.
 *
 * @date: 2022-06-25
 * @version: 1.0
 * @author: karry@haiwei.com
 */
public class test3_back {
    
    public static void main(String[] args) {
        try {
            final XWPFDocument document = HaiweiDocumentGenerator
                    .build(new File(FilePathUtils.getPath("33.docx")));
        
            Map<String, Object> map = new HashMap<>();
            Map<String, Object> order = new HashMap<>();
            
            order.put("no","订单编号");
            order.put("name","订单名称");
    
            List<Map<String, Object>> items = new ArrayList<>();
            Map<String, Object> item = new HashMap<>();
            item.put("name","鼠标");
            item.put("type","电子类");
            item.put("time","2018/8/26");
    
            items.add(item);
            items.add(item);
            items.add(item);
            items.add(item);
            order.put("items",items);
            map.put("order",order);
            HaiweiBeanUtil.setBean(map);
            HaiweiBeanUtil.setCallBackCache(null);
            
            // 处理
            Stack<HaiweiLoopStack> stack = new Stack<>();
            for(XWPFTable xwpfTable : document.getTables()){
    
                List<XWPFTableRow> xwpfTableRows = xwpfTable.getRows();
                for(int i  = 0 ; i < xwpfTableRows.size() ; i++){
                    XWPFTableRow row = xwpfTableRows.get(i);
    
                    HaiweiLoopStack loopStack1 = null;
                    if(!stack.empty()){
                        loopStack1 = stack.peek();
                    }
    
                    List<XWPFTableCell> cells = row.getTableCells();
                    
                    // 如果是1，并且是loop开始
                    if(cells.size() == 1){
                        String text = row.getTableCells().get(0).getText();
                        if(HaiweiLoopSginParser.isLoopStartSign(text)) {
                            HaiweiLoopSign loopSign = HaiweiLoopSginParser.parse(text);
                            if(loopSign != null){
                                final HaiweiLoopStack loopStack = new HaiweiLoopStack();
                                loopStack.setLoopSign(loopSign);
                                loopStack.setTable(xwpfTable);
                                loopStack.setSignStartIndex(i);
                                stack.push(loopStack);
                            }
                        }else if(HaiweiLoopSginParser.isLoopEndSign(text)){
                            // 如果是1，并且是loop结尾,需要挨个处理了
                            final HaiweiLoopStack loopStack = stack.pop();
                            loopStack.setSignEndIndex(i);
                            
                            // 获取数据量大小
                            int collectionLen = HaiweiBeanUtil.getCollectionLen(loopStack.getLoopSign().getPath());
                            // 模板数据行
                            final List<XWPFTableRow> templateRows = loopStack.getLoopTemplateRows();
                            // 模板数据行大小
                            int templateRowsSize = templateRows.size();
    
                            // 模板行本身占有一行，留着
                            int count = collectionLen - 1;
                            int rowIndex = -1;
                            while(count--  > 0){
                                rowIndex = templateRowsSize ;
                                while (--rowIndex >= 0 ){
                                    HaiweiXWPFTableUtil.addAndCopyRows(loopStack.getTable()
                                            ,templateRows.get(rowIndex)
                                            ,loopStack.getSignStartIndex()  + 1
                                            ,1);
                                }
                            }
                            loopStack.setSignEndIndex((collectionLen - 1) * templateRowsSize  + loopStack.getSignEndIndex());
                            HaiweiXWPFTableUtil.removeRow(loopStack.getTable(),loopStack.getSignEndIndex());
                            HaiweiXWPFTableUtil.removeRow(loopStack.getTable(),loopStack.getSignStartIndex());
                            i = loopStack.getSignEndIndex();
                        }
                    }else if(loopStack1 != null){
                        // loop 中
                        loopStack1.getLoopTemplateRows().add(row);
                    }
                    
                    
                    /*for(XWPFTableCell cell : cells){
    
                        // 表格
                        final List<XWPFTable> tables = cell.getTables();
                        if(tables != null && tables.size() > 0)
                            System.out.println(tables.get(0).getRow(0).getCell(0).getText());
                        // 表格
                        final String text = cell.getText();
                        
                        System.out.println(text);
                    }*/
                }
    
            }
           
            
            final File file = new File(FilePathUtils.getPath("3333.docx"));
            HaiweiDocumentGenerator.toFile(document,file);
            System.out.println(file.getAbsolutePath());
        
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
